package com.smg.community.thread;

import java.util.HashMap;
import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;

import com.smg.community.model.SmgMember;
import com.smg.community.model.SmgMemberCoor;
import com.smg.community.service.SmgMemberCoorManager;
import com.smg.community.service.SmgMemberManager;

public class SaveNearByMajiaThread extends Thread {

	@Override
	public void run() {
		
		try {
			while(true){
			HashMap a;
			List<SmgMember> majiaList = smgMemberManager.getMajiaList();
			int count  = 0;
			for(SmgMember member:majiaList){
				SmgMemberCoor coor = new SmgMemberCoor();
				float latitude = 	31.123823f;
				float longitdude = 121.36754f;
				if ( count > 50 ){
					latitude = 31.172566f;
					longitdude = 121.397869f;
				}else if ( count > 1000 ){
					latitude =  31.233463f;
					longitdude = 121.475419f;
				}else if (count > 1500){
					latitude = 31.197734f;
					longitdude = 121.407441f;
				}else if ( count > 2000 ){
					latitude = 31.250525f;
					longitdude = 121.455442f;
				}else if ( count >2500){
					latitude = 31.193145f;
					longitdude = 121.321589f;
				}
				Random random=new Random();//创建random对象　　　　
				float floatNumber=random.nextFloat();//获取一个浮点数(0-1)
				while(true){
					if ( floatNumber > 0 ){
						floatNumber = floatNumber/2000;
						break;
					}
					floatNumber=random.nextFloat() ;
				}
				if ( count /2 == 0) {
					latitude = latitude + floatNumber;
					longitdude = longitdude +	floatNumber;
				}else{
					latitude = latitude  - floatNumber;
					longitdude = longitdude -	floatNumber;
				}
				
				
				coor.setGpsLatitude(latitude);
				coor.setGpsLongitude(longitdude);
				coor.setMemberId(member.getMemberId());
				System.out.println("latitude:"+latitude+"  longitdude:"+longitdude);
				smgMemberCoorManager.save(coor);
			}
			sleep(1000 * 60 * 30);
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		super.run();
	}

	@Autowired
	private SmgMemberManager smgMemberManager;
	

	@Autowired
	private SmgMemberCoorManager smgMemberCoorManager;
	
	public SaveNearByMajiaThread(SmgMemberManager smgMemberManager,SmgMemberCoorManager smgMemberCoorManager ){
		this.smgMemberCoorManager = smgMemberCoorManager;
		this.smgMemberManager = smgMemberManager;
		
	}
	
	public static void main(String[] args){
		for(int i=0;i<10000;i++){				Random random=new Random();//创建random对象　　　　
		float floatNumber=random.nextFloat();//获取一个浮点数(0-1)
			System.out.println(floatNumber);
		}
		
	}
	
}
